package com.noodles.mall.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * mvc配置类
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    //图片存储位置
    @Value("${sys.fileUploadPath}")
    private String parentPath;

    @Value("${sys.filePathPatterns}")
    private String filePathPatterns;

    //跨域
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        System.out.println("WebMvcConfig: addCorsMappings 开始");
        System.out.println("registry: "+registry);
        registry.addMapping("/**")
                .allowedOrigins("*") // http://localhost:8080
                .allowedMethods("*")
                .allowedHeaders("*")
                .exposedHeaders(HttpHeaders.SET_COOKIE)
                .allowCredentials(true);
        System.out.println("WebMvcConfig: addCorsMappings 结束");
    }

    //访问图片, 映射图片路径
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        System.out.println("WebMvcConfig: addResourceHandlers 开始");
        System.out.println("registry: "+registry);
        registry.addResourceHandler(filePathPatterns).addResourceLocations("file:" + parentPath);
        System.out.println("WebMvcConfig: 结束");
    }
}
